package com.cy.app.bus;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cy.bean.news.News;
import com.cy.bean.scenic.Scenic;
import com.cy.service.bus.BusLineService;
import com.cy.service.news.NewsService;
import com.cy.service.scenic.ScenicService;
import com.cy.service.search.SearchService;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class AppAction {

	@Resource
	private SearchService searchService;
	@Resource
	private NewsService newsService;
	@Resource
	private ScenicService scenicService;
	@Resource
	private BusLineService busLineService;
	
	private int id;
	/* 查询条件 */
	private String q;
	
	/**
	 * 公交路线
	 * @return
	 */
	public String hotBusLine() {
		ActionContext.getContext().put("busLines", busLineService.getScrollData().getResultList());
		return "hotBusLine";
	}
	
	/**
	 * 热门车站
	 * @return
	 */
	public String hotStation() {
		@SuppressWarnings("rawtypes")
		List stations = searchService.searchHotStations();
		ActionContext.getContext().put("stations", stations);
		return "hotStation";
	}
	
	/**
	 * 发现栏目的详细内容
	 * @return
	 */
	public String findDetail() {
		Scenic scenic = scenicService.find(id);
		ActionContext.getContext().put("scenic", scenic);
		return "findDetail";
	}
	
	/**
	 * 新闻栏目的详细内容
	 * @return
	 */
	public String newsDetail() {
		News news = newsService.find(id);
		ActionContext.getContext().put("news", news);
		return "newsDetail";
	}
	
	/**
	 * 首页
	 * @return
	 */
	public String home() {
		return "home";
	}
	
	/**
	 * 新闻
	 * @return
	 */
	public String news() {
		// 用于存放排序的条件
		LinkedHashMap<String, String> orderby = new LinkedHashMap<>();
		// 按照id降序排列
		orderby.put("id", "desc");
		ActionContext.getContext().put("result", newsService.getScrollData(1, 50,orderby).getResultList());
		return "news";
	}
	
	/**
	 * 发现
	 * @return
	 */
	public String find() {
		// 用于存放排序的条件
		LinkedHashMap<String, String> orderby = new LinkedHashMap<>();
		// 按照id降序排列
		orderby.put("id", "desc");
		ActionContext.getContext().put("result", scenicService.getScrollData(-1, -1,orderby).getResultList());
		return "find";
	}
	
	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		// 处理中文乱码
		String temp = null;
		try {
			temp = new String(q.trim().getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
		}
		this.q = temp;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
}
